﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Single_SignOn.Models;
using Single_SignOn.Attributes;
using Single_SignOn.Json;
using Single_SignOn.ResX;
using UserProfile = Single_SignOn.Models.UserProfile;

namespace Single_SignOn.Areas.Handheld.Controllers
{
    public class LoginController : Controller
    {
        //
        // GET: /Handheld/Login/

        public ActionResult Index()
        {
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [JsonFilter(Param = "oLoginDataObj", RootType = typeof(oSubmitDataContract))]
        public ActionResult Login(oSubmitDataContract oLoginDataObj)
        {

            try
            {
                JsonResult js = new JsonResult();
                DataAccess.SingleSignOn.AuthenConnection oAuth = new DataAccess.SingleSignOn.AuthenConnection();
                DataTable dt =
                    // Called Data Access
                   oAuth.pullUserProfile(oLoginDataObj.Username, th.co.homepro.Utility.zEncrypt(oLoginDataObj.Password, WebConfig.Encryptkey, WebConfig.EncryptValue));
                
                if (dt != null)
                    if (dt.Rows.Count > 0)
                    {

                        DataTable dtUserData = oAuth.pullUserProfile(oLoginDataObj.Username);
                        UserProfile oUserProfile;
                        if (dtUserData != null)
                            if (dtUserData.Rows.Count > 0)
                            {
                                Session.Remove("UserInfo");
                                oUserProfile = new UserProfile();
                                oUserProfile.Description = (System.DBNull.Value == dtUserData.Rows[0]["DESCRIPTION"]) ? "" : (string)dtUserData.Rows[0]["DESCRIPTION"];
                                oUserProfile.GroupDescription = (System.DBNull.Value == dtUserData.Rows[0]["GROUPDESC"]) ? "" : (string)dtUserData.Rows[0]["GROUPDESC"];
                                oUserProfile.EmployeeNo = (System.DBNull.Value == dtUserData.Rows[0]["EMPID"]) ? "" : (string)dtUserData.Rows[0]["EMPID"];
                                oUserProfile.Username = (System.DBNull.Value == dtUserData.Rows[0]["EMPNO"]) ? "" : (string)dtUserData.Rows[0]["EMPNO"];
                                oUserProfile.FullName = (System.DBNull.Value == dtUserData.Rows[0]["EMPNAME"]) ? "" : (string)dtUserData.Rows[0]["EMPNAME"];
                                oUserProfile.Password = (System.DBNull.Value == dtUserData.Rows[0]["PASSWORD"]) ? "" : (string)dtUserData.Rows[0]["PASSWORD"];
                                oUserProfile.GroupNo = (System.DBNull.Value == dtUserData.Rows[0]["GROUPNO"]) ? "" : (string)dtUserData.Rows[0]["GROUPNO"];
                                oUserProfile.Division = (System.DBNull.Value == dtUserData.Rows[0]["DIVISION"]) ? "" : (string)dtUserData.Rows[0]["DIVISION"];
                                oUserProfile.Store = (System.DBNull.Value == dtUserData.Rows[0]["STORENO"]) ? "" : (string)dtUserData.Rows[0]["STORENO"];
                                oUserProfile.Tel = (System.DBNull.Value == dtUserData.Rows[0]["TEL"]) ? "" : (string)dtUserData.Rows[0]["TEL"];
                                oUserProfile.Email = (System.DBNull.Value == dtUserData.Rows[0]["EMAIL"]) ? "" : (string)dtUserData.Rows[0]["EMAIL"];
                                oUserProfile.Status = (System.DBNull.Value == dtUserData.Rows[0]["STATUS"]) ? "" : (string)dtUserData.Rows[0]["STATUS"];
                                oUserProfile.ApplNo = (System.DBNull.Value == dtUserData.Rows[0]["APPLNO"]) ? "" : (string)dtUserData.Rows[0]["APPLNO"];
                                oUserProfile.ProfileLevel = (System.DBNull.Value == dtUserData.Rows[0]["PROFILELEVEL"]) ? "" : (string)dtUserData.Rows[0]["PROFILELEVEL"];
                                oUserProfile.Language = (System.DBNull.Value == dtUserData.Rows[0]["LANG"]) ? "" : (string)dtUserData.Rows[0]["LANG"];
                                Session["UserInfo"] = oUserProfile;
                            }
                        //*
                        UserProfile o = new UserProfile();
                        js = Json(new
                        {
                            redirectToUrl = Url.Action("Index"
                                , "ApplicationList"
                                , (UserProfile)o
                                , "http")
                             ,
                            exception = ""
                        });
                    }
                    else
                    {
                        js = Json(new
                        {
                            redirectToUrl = Url.Action("Index"
                                , ""
                                , ""
                                , "http")
                             ,
                            exception = ResourceTools.GetResource("Resource", "DataNotFound")
                        });
                    }

                else
                {
                    js = Json(new
                    {
                        redirectToUrl = Url.Action("Index"
                            , ""
                            , ""
                            , "http")
                                 ,
                        exception = ResourceTools.GetResource("Resource", "DataNotFound")
                    });
                }


                return js;
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        [AllowAnonymous]
        public ActionResult LogOut()
        {
            Session.Remove("UserInfo");
            Session["UserInfo"] = null;
            return RedirectToAction("Index", "Login");
        }

    }
}
